package com.jiaai.web.member;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.exception.ServerException;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.member.api.IFollowAPI;
import com.jiaai.util.ConstantUtils;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title FollowController
 * @since 2017/11/13
 */
@RequestMapping("/api/v1/app/follow")
@RestController
public class FollowController {
    @Reference
    private IFollowAPI followAPI;

    //我关注的人
    @GetMapping("/list")
    @TokenIntercept(checkVisitor = true)
    public  Object myFollow(@RequestParam Integer curr)throws ServerException{
        AccessUser user = RequestUtils.getUser();
        return RestResultGenerator.genSuccessResult(followAPI.findMyFollowMember(user.getUserId(), curr));
    }
    //我的粉丝
    @GetMapping("/fans")
    @TokenIntercept(checkVisitor = true)
    public Object findFans(@RequestParam Integer curr)throws  ServerException{
        AccessUser user = RequestUtils.getUser();
        return  RestResultGenerator.genSuccessResult(followAPI.findFansByMId(user.getUserId(), curr));
    }
    //flag 1:关注 0:取消关注
    @PostMapping("/{to}")
    @TokenIntercept(checkVisitor = true)
    public  Object follow(@PathVariable("to") String to)throws ServerException{
        AccessUser user = RequestUtils.getUser();
        followAPI.followOrUnfollow(user.getUserId(),to, 1);
        return RestResultGenerator.genSuccessResult();
    }


    //flag 1:关注 0:取消关注
    @PostMapping("/cancel/{to}")
    @TokenIntercept(checkVisitor = true)
    public  Object cancelFollow(@PathVariable("to") String to)throws ServerException{
        AccessUser user = RequestUtils.getUser();
        followAPI.followOrUnfollow(user.getUserId(),to, 0);
        return RestResultGenerator.genSuccessResult();
    }




    @GetMapping("/init/{mId}")
    public  Object init(@PathVariable("mId") String mId)throws ServerException{
        return RestResultGenerator.genSuccessResult(followAPI.findByAll(mId));
    }
    @GetMapping("/cancel/{to}")
    public  Object cancel(@SessionAttribute(value = ConstantUtils.ACCESS_USER, required = false) AccessUser user, @PathVariable("to") String to)throws ServerException{
        followAPI.cancelFollow(user.getUserId(),to);
        return RestResultGenerator.genSuccessResult();
    }



}
